<?
require_once("functions.php");


/*******************************************************
Function:	enableQuickPicks($ID,$League)
Updated:	03/17/2008
Description:	Adds the user & initial team rankings to the ".$League."QuickPicks Table for use in quick Picks system
********************************************************/
function enableQuickPicks($ID,$League)
{
	//Select All Teams Sorted By Wins, Loses, PointsFor, & PointsAgainst
	$SQL = "SELECT TeamName, Wins, Loses, PointsFor, PointsAgainst FROM ".$League."Teams ORDER BY Wins DESC, Loses ASC, PointsFor DESC, PointsAgainst ASC";
	$weekresult = mysql_query($SQL) or die("Cannot query the database to get team list.<br/>");
	// Begin the SQL query to insert new record
	$PickSQL = "INSERT INTO ".$League."QuickPicks Values ($ID,";
	while($myrow = mysql_fetch_array($weekresult))	//Loop through list of teams sorted by record & points
	{
		if ($myrow["TeamName"] != "")
			$PickSQL.= "'".$myrow["TeamName"]."',"; //Add Each team to the insert querys
	}
	$PickSQL = substr($PickSQL, 0, strlen($PickSQL)-1);	//Remove the last comma from last field to be inserted.
	$PickSQL.= ")";
	mysql_query($PickSQL) or die("Cannot query the database to insert quick picks.<br/>");
}

/*******************************************************
Function:	makeSysQuickPicks($Week,$League)
Updated:	03/17/2008
Description:	Finds all NULL picks for each user, use the quick picks rankings to make picks
********************************************************/
function makeSysQuickPicks($Week,$League)
{
	// Get all User IDs from the ".$League."QuickPicks table, I.E. all users with QP feature enabled.
	if (getWeek() > 17)		//If Playoff Time
		$SQL = "SELECT ".$League."QuickPicks.UserID, Users.PlayOffs, ".$League."Users.UserID FROM ".$League."QuickPicks, ".$League."Users,$League WHERE ((".$League."QuickPicks.UserID = ".$League."Users.UserID) AND (Users.PlayOffs = 1))";
	else
		$SQL = "SELECT UserID FROM ".$League."QuickPicks";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	while($myrow = mysql_fetch_array($result))	//Loop through list of Users, make Quick Picks for each
	{	
		echo "In SysQuickPicks, userID = ".$myrow["UserID"]."<br/>";
		if ($myrow["UserID"] != "")
			makeUserQuickPicks($myrow["UserID"],$Week,$League);
	}
	
}


/*******************************************************
Function:	makeUserQuickPicks($ID, $Week)
Updated:	03/17/2008
Description:	Use the quick picks rankings to make picks for this user.
********************************************************/
function makeUserQuickPicks($ID, $Week,$League)
{
	// Select the Teams (by rank) out of the ".$League."QuickPicks table
	$SQL = "SELECT * FROM ".$League."QuickPicks WHERE UserID = $ID";
	$results = mysql_query($SQL) or die("Cannot query the database.<br/>");	
	$myRow=mysql_fetch_array($results);
	for ($num = 0; $num < 32; $num++)
		$Teams[$num]=$myRow["Team".($num+1)];	//Fill Array With Data

	// Determine if this is a pick UPDATE, or a pick INSERT
	$UPDATE = false;
	$SQL = "SELECT UserID FROM ".$League."Picks WHERE UserID=$ID AND Week=$Week";
	$results = mysql_query($SQL) or die("Cannot query the database.<br/>");
	if(!mysql_num_rows($results))
	{	//This is the first time any picks have been made for the week.  Query will be straight INSERT.
		InsertUserQuickPicks($ID, $Week, $Teams,$League);
		return;
	}
	
	// Select All the Games for the week
	$SQL = "SELECT Home,Away,GameNum,PickEmailed FROM ".$League."Schedule WHERE Week=$Week ORDER BY GameNum ASC";
	$results = mysql_query($SQL) or die("Cannot query the database.<br/>");
	// Begin the UPDATE query
	$PickSQL = "UPDATE ".$League."Picks SET ";
		
	do{
		if (($myRow["GameNum"] != Null) || ($myRow["GameNum"] != ""))
		{	
			if ($myRow["PickEmailed"] == 0)
			{
				//See If User has already made a pick for this game
				$InSQL = "SELECT Game".$myRow["GameNum"]." FROM ".$League."Picks WHERE UserID=$ID AND Week=$Week";
				$InResults = mysql_query($InSQL) or die("Cannot query the database.<br/>");
				$InRow = mysql_fetch_array($InResults);
				if (($InRow["Game".$myRow["GameNum"]] == "") || ($InRow["Game".$myRow["GameNum"]] == NULL))
					$pick = getQP($Teams,$myRow["Home"],$myRow["Away"]);
				else
					$pick = $InRow["Game".$myRow["GameNum"]];			
			
				$PickSQL.= "Game".$myRow["GameNum"]."='$pick', ";
				echo "Game ".$myRow["GameNum"]."  pick = $pick<br/>";
			}
			else	//If pick was emailed the game is in progress or over and cannot be changed
				echo "Game ".$myRow["GameNum"]." has already begun or ended.  It cannot be changed.<br/>";
		}
	}while($myRow = mysql_fetch_array($results));
	//REMOVE COMMA & SPACE FROM LAST VALUE
	$PickSQL = substr($PickSQL, 0, strlen($PickSQL)-2);	
	// End the UPDATE query
	$PickSQL = $PickSQL." WHERE UserID=$ID AND Week=$Week";
	
	// Perform the query
	mysql_query($PickSQL) or die("Cannot query the database.<br/>");
}

/*******************************************************
Function:	InsertUserQuickPicks($ID, $Week, $Teams)
Updated:	03/17/2008
Description:	Use the quick picks rankings to make picks for this user.  
				Insert the the picks for the first time into ".$League."Picks table
********************************************************/
function InsertUserQuickPicks($ID, $Week, $Teams,$League)
{
	// Select All the Games for the week
	$SQL = "SELECT Home,Away,GameNum,PickEmailed FROM ".$League."Schedule WHERE Week=$Week ORDER BY GameNum ASC";
	$results = mysql_query($SQL) or die("Cannot query the database.<br/>");
	// Begin the INSERT query
	$PickSQL = "INSERT INTO ".$League."Picks (UserID, Week";
	for($i = 1; $i <= mysql_num_rows($results); $i++)
		$PickSQL.= ", Game$i";
	$PickSQL.= ") VALUES ($ID, $Week, ";

	do{
		if (($myRow["GameNum"] != Null) || ($myRow["GameNum"] != ""))
		{
			$pick = getQP($Teams,$myRow["Home"],$myRow["Away"]);
			if ($myRow["PickEmailed"] != 0)	//If pick was emailed the game is in progress or over and cannot be changed
			{
				$PickSQL.= "'-', ";
				echo "Game ".$myRow["GameNum"]." has already begun or ended.  It cannot be changed.<br/>";
			}
			else
			{
				$PickSQL.= "'$pick', ";
				echo "Game ".$myRow["GameNum"]."  pick = $pick<br/>";
			}
		}
	}while($myRow = mysql_fetch_array($results));
	//REMOVE COMMA & SPACE FROM LAST VALUE
	$PickSQL = substr($PickSQL, 0, strlen($PickSQL)-2);	
	// End the INSERT query
	$PickSQL = $PickSQL.")";
	// Perform the query
	mysql_query($PickSQL) or die("Cannot query the database.<br/>");
}

/*******************************************************
Function:	getQP(&$arr,$home,$away)
Updated:	03/17/2008
Description:	Returns the higher ranked team.  (Whichever team has the lower array index)
********************************************************/
function getQP($arr,$home,$away)
{
	if (array_search($home, $arr) < array_search($away, $arr))
		return $home;
	else
		return $away;
}

/*******************************************************
Function:	IsQPEnabled($ID,$League)
Updated:	03/17/2008
Description:	Returns true if user has Quick Picks Enabled
********************************************************/
function IsQPEnabled($ID,$League)
{
	//Select All Teams Sorted By Wins, Loses, PointsFor, & PointsAgainst
	$SQL = "SELECT UserID FROM ".$League."QuickPicks WHERE UserID=$ID";
	$result = mysql_query($SQL) or die("Cannot query the database.<br/>");
	if($myrow = mysql_fetch_array($result))
		return true;
	else
		return false;
}

/*******************************************************
Function:	ReOrderByRecord($ID,$League)
Updated:	03/17/2008
Description:	Returns true if user has Quick Picks Enabled
********************************************************/
function ReOrderByRecord($ID,$League)
{
	//Select All Teams Sorted By Wins, Loses, PointsFor, & PointsAgainst
	$SQL = "SELECT TeamName, Wins, Loses, DivWins, DivLosses, PointsFor, PointsAgainst FROM ".$League."Teams ORDER BY Wins DESC, Loses ASC, DivWins DESC, DivLosses ASC, PointsFor DESC, PointsAgainst ASC";
	$weekresult = mysql_query($SQL) or die("Cannot query the database.<br/>");
	// Begin the SQL query to insert new record
	$PickSQL = "UPDATE ".$League."QuickPicks SET ";
	$Num = 1;
	while($myrow = mysql_fetch_array($weekresult))	//Loop through list of teams sorted by record & points
	{
		if ($myrow["TeamName"] != "")
		{
			$PickSQL.= "Team$Num='".$myrow["TeamName"]."',"; //Add Each team to the insert querys
			$Num++;
		}
	}
	$PickSQL = substr($PickSQL, 0, strlen($PickSQL)-1);	//Remove the last comma from last field to be inserted.
	$PickSQL.= " WHERE UserID=$ID";
	mysql_query($PickSQL) or die("Cannot query the database.<br/>");
}


?>